package com.japher.hackson.activity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.baidu.common.async_http.IResponseListener;
import com.baidu.common.json.JsonSerializer;
import com.baidu.common.utils.TimeUtils;
import com.baidu.common.utils.Tools;
import com.baidu.common.views.LoadingDialog;
import com.common.image_loader.ImageManager;
import com.common.image_loader.ImageManager.RequestImageListener;
import com.japher.hackson.R;
import com.japher.hackson.document.CommentItem;
import com.japher.hackson.document.DetailInfo;
import com.japher.hackson.requests.LoadDetailRequest;
import com.japher.hackson.response.BaseResponse;
import com.japher.hackson.response.DetailInfoResponse;

public class DetailActivity extends Activity implements OnClickListener{

    private LoadingDialog mLoadingView;
    private TextView mUserComment;
    private ImageView mUserImage;
    private TextView mTime;
    private CommentItem externalItem;
    private ScrollView mCommentScrollView;
    private LinearLayout mCommentLayout;
    private Context mContext;
    private Button commit;
    private LinearLayout layout_input;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_detail);
        mContext = this;
        mLoadingView = new LoadingDialog(this, false, null);
        mLoadingView.setMessage("正在加载数据...");
        
        mUserComment = (TextView)findViewById(R.id.user_comment);
        mUserImage = (ImageView)findViewById(R.id.user_img);
        mTime = (TextView)findViewById(R.id.user_time);
        mCommentScrollView = (ScrollView)findViewById(R.id.layout_sc);
        mCommentScrollView = (ScrollView)findViewById(R.id.layout_sc);
        mCommentLayout = (LinearLayout)findViewById(R.id.layout_content);
        commit =  (Button)findViewById(R.id.commit);
        commit.setOnClickListener(this);
        layout_input = (LinearLayout)findViewById(R.id.layout_input);
        
        String json = getIntent().getStringExtra("data");
        if(!Tools.isEmpty(json))
        {
            externalItem = JsonSerializer.getInstance().deserialize(json,CommentItem.class);
        }
        
        if(externalItem != null){
            mUserComment.setText(externalItem.getBody());
            mTime.setText(TimeUtils.getDateTile(externalItem.getCreatedAt()));
            
            loadRemote(externalItem.get_id());
        }
        else{
            finish();
            return;
        }
        
    }
    
    private DetailInfo mDetailInfo;
    
    private void loadRemote(String id){
        
        mLoadingView.show();
        
        LoadDetailRequest request = new LoadDetailRequest(id);
        request.StartRequest(new IResponseListener() {
            @Override
            public void onRequestComplete(BaseResponse response) {
                
                mLoadingView.dismiss();
                if(response != null && response.isSuccess() && response instanceof DetailInfoResponse){
                    DetailInfoResponse res = (DetailInfoResponse)response;
                    
                    setup(res.getData());
                }
                else{
                    DetailInfo info = new DetailInfo();
                    DetailInfo.Image[] images = new DetailInfo.Image[1] ;
                    
                    DetailInfo.Image im = new DetailInfo.Image();
                    im.setUri("http://z1.dfcfw.com/2011/7/7/201107071055371811565487.jpg");
                    images[0] = im;
                    info.setImages(images);
                    
                    DetailInfo.Comment[] commnets = new DetailInfo.Comment[9];
                    for(int i = 0 ; i < commnets.length ; i++){
                        DetailInfo.Comment cc = new  DetailInfo.Comment ();
                        cc.setBody("这里的东西很便宜" + i);
                        commnets[i] = cc;
                    }
                    info.setComments(commnets);
                    setup(info);
                }
            }
        });
        
    }
    
    private void setup(DetailInfo info){
        
        if(info == null){
            return;
        }
        
        final DetailInfo.Image[] images = info.getImages();
        if(images != null && images.length > 0){
            DetailInfo.Image image = images[0];
            
            if(!Tools.isEmpty(image.getUri())){
                
                ImageManager.getImage(image.getUri(), new RequestImageListener() {
                    
                    @Override
                    public void onRecievedImage(Bitmap bitmap) {
                        mUserImage.setImageBitmap(bitmap);
                        mUserImage.invalidate();
                    }
                    
                    @Override
                    public void onProgress(int progress) {
                        
                    }
                });
            }
        }
        mCommentLayout.removeAllViews();
        final DetailInfo.Comment[] comments = info.getComments();
        if(comments != null && comments.length > 0){
            for(int i = 0 ; i < comments.length ; i++){
                
                FrameLayout view = (FrameLayout)LayoutInflater.from(mContext).inflate(R.layout.layout_comment, null);
                
                DetailInfo.Comment comment = comments[i];
                TextView textView = (TextView)view.findViewById(R.id.user_commnet);
                
                textView.setText(comment.getBody());
                mCommentLayout.addView(view);
            }
        }
    }

    @Override
    public void onClick(View v) {
        layout_input.setVisibility(View.VISIBLE);
    }
    
}
